<template>
	<view>
		<view class="top">

		</view>
		<view class="bottom">
			<view class="bottom-card1">
				<template v-if="ordersList.length > 0">
					<view class="xiaofei" v-for="(item, index) in ordersList" :key="index">
						<view>
							<view class="font1">
								商家名称: {{item.merchantName}}
							</view>
							<view class="font1">
								用户姓名: {{item.nickName}}
							</view>
							<view class="font2">
								{{item.orderTime}}
							</view>
						</view>
						<view class="xiaofei2">
							-{{item.orderPrice}}
						</view>
					</view>
				</template>
				<view v-else class="empty-state">
					暂无订单记录
				</view>
			</view>
		</view>

	</view>
</template>

<script>
import request from '../../../uitls/request';

	export default {
		data() {
			return {
				ordersList: [],
				userInfo: null,
				userId: null,
				merchantList: []
			}
		},
		methods: {
			getMerchantName(merchantId) {
				const merchant = this.merchantList.find(m => m.id === merchantId);
				return merchant ? merchant.name : '未知商家';
			}
		},
		onLoad() {
			// 获取用户信息
			const userInfo = uni.getStorageSync('userInfo');
			this.userInfo = userInfo;
			this.userId = userInfo?.id;
			
			// 输出用户ID到控制台
			console.log('当前用户ID:', this.userId);
			console.log('完整用户信息:', this.userInfo);		
			request({
				url: '/xsc/orders/list',
				method: 'GET',
				data: {
					pageNum: 1,
					pageSize: 100
				},
				success: (res) => {
					const result = res.data;
					if (result.code == 200) {
						// 过滤出当前用户的订单
						const userOrders = res.data.rows.filter(order => order.userId === this.userId);
						
						// Add current user's nickName and merchant name to each order object
						this.ordersList = userOrders.map(order => {
							return { ...order, nickName: this.userInfo?.nickName, merchantName: this.getMerchantName(order.merchantId) };
						});
						
						console.log('过滤后的订单列表:', this.ordersList);
					}
				}
			});
			// 获取商家列表
			request({
					url: '/xsc/merchant/list',
					method: 'GET',
					data: {
						pageNum: 1,
						pageSize: 100
					},
					success: (res) => {
						const result = res.data;
						if (result.code == 200) {
							this.merchantList = res.data.rows;
							console.log('商家列表:', this.merchantList);
							
							// Re-process orders if they were loaded before merchants
							if (this.ordersList.length > 0) {
								this.ordersList = this.ordersList.map(order => {
									return { ...order, merchantName: this.getMerchantName(order.merchantId) };
								});
							}
						}
					}
			})
		}
	}
</script>

<style>
	.top {
		width: 100%;
		height: 200px;
		background: linear-gradient(to right, #FF7500, #FF9500);
		position: relative;
		overflow: hidden;
	}

	.top::after {
		content: '';
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: linear-gradient(45deg, rgba(255,255,255,0.1) 25%, transparent 25%),
					linear-gradient(-45deg, rgba(255,255,255,0.1) 25%, transparent 25%);
		background-size: 20px 20px;
	}

	.bottom {
		margin: 0 20px;
	}

	.bottom-card1 {
		min-height: 500px;
		margin-top: -150px;
		background-color: #fff;
		border-radius: 15px;
		box-shadow: 0 4px 20px rgba(0,0,0,0.1);
		padding: 20px;
		position: relative;
		z-index: 1;
	}

	.xiaofei {
		height: auto;
		margin: 0;
		padding: 15px 0;
		border-bottom: 1px solid #F0F0F0;
		display: flex;
		justify-content: space-between;
		align-items: center;
		transition: all 0.3s ease;
	}

	.xiaofei:last-child {
		border-bottom: none;
	}

	.xiaofei:active {
		background-color: #F8F8F8;
	}

	.font1 {
		margin-bottom: 5px;
		font-size: 16px;
		color: #333;
		font-weight: 500;
	}

	.font2 {
		font-size: 14px;
		color: #999;
		margin-top: 5px;
	}

	.xiaofei2 {
		font-size: 18px;
		color: #FF7500;
		font-weight: 600;
	}

	/* 添加空状态提示 */
	.empty-state {
		text-align: center;
		padding: 40px 0;
		color: #999;
		font-size: 14px;
	}
</style>